/*
 缩进、反缩进的plugin
 */
(function(){
    var whitespaceHTML = ' ';
    var indentHTML = '    '; //四个空格键
	var domUtils = baidu.editor.dom.domUtils,
		block = domUtils.isBlockElm,
		utils = baidu.editor.utils,
		setIndent = function(range, type){
			var bookmark = range.createBookmark(),
				filterFn = function( node ) {
					return node.nodeType == 1 ? node.tagName.toLowerCase() != 'br' &&  !domUtils.isBookmarkNode(node) : !domUtils.isWhitespace( node )
				};
			range.enlarge(true);
			var bookmark2 = range.createBookmark(),
				current = domUtils.getNextDomNode(bookmark2.start,false,filterFn),
				tmpRange = range.cloneRange(),
				tmpNode;
			
			while(current &&  !(domUtils.getPosition(current,bookmark2.end)&domUtils.POSITION_FOLLOWING)){
				if(current.nodeType == 3 || !block(current)){
					tmpRange.setStartBefore(current);
					while(current && current!==bookmark2.end &&  !block(current)){
						tmpNode = current;
						current = domUtils.getNextDomNode(current,false,null,function(node){
							return !block(node)
						});
					}
					tmpRange.setEndAfter(tmpNode);
					var common = tmpRange.getCommonAncestor();
					if( !domUtils.isBody(common) && block(common)){
						var sp = common.getElementsByTagName('span'),
							whitespace = null,
							splen = sp.length;
						for(var i = 0; i < splen; i++){
							if(sp[i].className == 'whitespace'){
								whitespace = sp[i];
								break;
							}
						}
						if( whitespace && whitespace.className == 'whitespace'){
							if(type == 'indent'){
								whitespace.innerHTML = indentHTML + whitespace.innerHTML
							}else{
								var space =  whitespace.innerHTML.replace(indentHTML, '');
								whitespace.innerHTML = space;
							}
							current = common;
						}else{
							var p = range.document.createElement('p');
							var span = range.document.createElement('span');
							span.className = 'whitespace';
							span.style.cssText = 'white-space:pre';
							span.innerHTML = indentHTML;
							p.appendChild(span);
							var frag = tmpRange.extractContents();
							p.appendChild(frag);
							tmpRange.insertNode(p);
							if(p.parentNode && p.parentNode.tagName == 'P'){
								domUtils.remove( p.parentNode, true );
							}
							current = p;
						}
					}else{
						var p = range.document.createElement('p');
						p.innerHTML = '<span class="whitespace" style="white-space:pre">' + indentHTML + '</span>';
						var frag = tmpRange.extractContents();
						p.appendChild(frag);
						tmpRange.insertNode(p);
						current = p;
					}
					current = domUtils.getNextDomNode(current,false,filterFn);
				}else{
					current = domUtils.getNextDomNode(current,true,filterFn);
				}
			}
			return range.moveToBookmark(bookmark2).moveToBookmark(bookmark)
		};
	function indentUI( cmdName, type ){
		range = this.selection.getRange();
		var startNode = this.selection.getStart();
		if(startNode.tagName.toLowerCase() == 'span' && startNode.className == 'whitespace'){
			range.enlarge(true);
		}
		if(range.collapsed){
			var txt = this.document.createTextNode('p');
			range.insertNode(txt);
		}
		setIndent(range, type);
		if(txt){
			range.setStartBefore(txt).collapse(true);
			domUtils.remove(txt);
		}
		range.select();

	}
	baidu.editor.plugins['textIndent'] = function(){
		var editor = this,
			inSpacing = 0,
			lastSpaceNode = null;
		editor.addListener('keydown', function(type, e ){
			var code = e.keyCode;
			if(code == 9){
				domUtils.preventDefault(e);
				if(e.shiftKey){
					indentUI.call(editor, 'tabindent', 'outdent');
				}else{
					indentUI.call(editor, 'tabindent', 'indent');
				}
			}
            
			
		});
	}
})();